package com.shangyueshang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shangyueshang.common.SysConstants;
import com.shangyueshang.mapper.GoodsAdvertiseFeeMapper;
import com.shangyueshang.mapper.GoodsRevenueMapper;
import com.shangyueshang.mapper.GoodsStorageFeeMapper;
import com.shangyueshang.model.GoodsAdvertiseFee;
import com.shangyueshang.model.GoodsRevenue;
import com.shangyueshang.model.GoodsStorageFee;
import com.shangyueshang.request.PageReq;
import com.shangyueshang.response.GoodsRevenueExportDTO;
import com.shangyueshang.response.PageResult;
import com.shangyueshang.service.GoodsRevenueService;
import com.shangyueshang.utils.CollectionUtil;
import com.shangyueshang.utils.ExcelUtil;
import com.shangyueshang.utils.MyPageHelper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/17 19:05
 * @version: v1.0.0
 */
@Slf4j
@Service
public class GoodsRevenueServiceImpl extends ServiceImpl<GoodsRevenueMapper, GoodsRevenue> implements GoodsRevenueService {
	
	@Resource
	private GoodsAdvertiseFeeMapper goodsAdvertiseFeeMapper;
	
	@Resource
	private GoodsStorageFeeMapper goodsStorageFeeMapper;
	
	@Override
	public PageResult<GoodsRevenue> findPage(PageReq pageReq) {
		Map<String, Object> lineParams = pageReq.getLineParams();
		PageInfo<GoodsRevenue> goodsRevenuePage =
				PageHelper.startPage(
						pageReq.getPageNum(),
						pageReq.getPageSize()
				).doSelectPageInfo(
						() -> getBaseMapper().countSales(lineParams)
				);
		//获取广告费、仓储费
		List<YearMonth> tradeMonths = goodsRevenuePage.getList().stream().map(GoodsRevenue::getTradeMonth).distinct().collect(Collectors.toList());
		Map<String, Object> params;
		for (YearMonth tradeMonth : tradeMonths) {
			params = Collections.singletonMap(SysConstants.trade_month, tradeMonth);
			List<GoodsAdvertiseFee> advertiseFees = goodsAdvertiseFeeMapper.queryAdvertiseFee(params);
			advertiseFees.forEach(
					af -> goodsRevenuePage.getList()
							.forEach(
									ll -> {
										if (af.shopSkuKey().equals(ll.shopSkuKey())
												&& Objects.equals(af.getTradeMonth(), ll.getTradeMonth())) {
											ll.setAdvertiseFee(BigDecimal.ZERO.subtract(af.getCost()));
										}
									}
							)
			);
			//获取上个月的仓储费
			params = Collections.singletonMap(SysConstants.trade_month, tradeMonth.minusMonths(1L));
			List<GoodsStorageFee> goodsStorageFees = goodsStorageFeeMapper.queryGoodsStorageFee(params);
			goodsStorageFees.forEach(
					af -> goodsRevenuePage.getList()
							.forEach(
									ll -> {
										if (af.shopSkuKey().equals(ll.shopSkuKey())
												&& Objects.equals(af.getTradeMonth(), ll.getTradeMonth().minusMonths(1L))) {
											ll.setLastMonthStorageFee(BigDecimal.ZERO.subtract(af.getEstimatedMonthlyStorageFee()));
										}
									}
							)
			);
		}
		
		return MyPageHelper.getPageResult(goodsRevenuePage);
	}
	
	@SneakyThrows
	@Override
	public ResponseEntity<org.springframework.core.io.Resource> goodsRevenueExport(PageReq pageReq) {
		//设置最多导出5000行
		pageReq.setPageSize(ExcelUtil.EXPORT_LIMIT);
		PageResult<GoodsRevenue> pageResult = this.findPage(pageReq);
		List<GoodsRevenueExportDTO> exportList = CollectionUtil.beanCopyList(pageResult.getContent(), GoodsRevenueExportDTO::new);
		return ExcelUtil.getExcelSimpleResponseEntity(Collections.emptyList(), exportList, GoodsRevenueExportDTO.class);
	}
	
	@Override
	public GoodsRevenue getByShopSkuTradeDate(String shop, String sku, LocalDate date) {
		return super.getOne(
				new QueryWrapper<GoodsRevenue>()
						.eq(SysConstants.shop, shop)
						.eq(SysConstants.sku, sku)
						.eq(SysConstants.trade_date, date)
		);
	}
}
